package com.ctg.payroll2.action.social;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.filter.social.CsEmplsocialFilter;
import com.ctg.payroll2.bean.filter.social.PbEmployeeFilter;
import com.ctg.payroll2.bean.po.PbEmployee;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.bean.po.social.CsEmplsocial;
import com.ctg.payroll2.bean.po.social.CsSocialDetail;
import com.ctg.payroll2.bean.po.social.CsSocialMaster;
import com.ctg.payroll2.bean.po.social.PbCommsocialMaster;
import com.ctg.payroll2.bean.po.social.PbSocialDetail;
import com.ctg.payroll2.bean.po.social.PbSocialMasterAndDetail;
import com.ctg.payroll2.service.social.CsEmplsocialService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 员工社保档案管理
 * @author 鲁力 2009-9-1 下午07:34:27
 *
 */
@SuppressWarnings("unchecked")
public class CsEmplsocialAction extends ActionSupport {
	private static final long serialVersionUID = -9092405647838533356L;
	private String msg ;
	protected Log logger = LogFactory.getLog(this.getClass());
	private CsEmplsocial csEmplsocial;
	private String successFlag;
	
	private Page csEmplsocialResult;
	private Page csSocialResult;
	private Page pbEmployeeResult;
	
	private CsEmplsocialFilter filter;
	/**
	 * 员工查询的查询条件过滤器
	 */
	private PbEmployeeFilter pbEmployeeFilter;
	
	private CsEmplsocialService service;
	
	private PbSocialMasterAndDetail pbSocialMasterAndDetail;
	
	private PbCommsocialMaster pbCommsocialMaster;//推荐模板
	
	private PbSocialDetail pbSocialDetail;//推荐模板细项
	
	private CsSocialMaster csSocialMaster;//客户模板
	
	private CsSocialDetail csSocialDetail;//客户模板细项
	
	private String empSsIds;

	private CsEmplsocial csEmplsocial1;
	private PbEmployee pbEmployee1;
	/**
	 * 推荐模板细项
	 */
	private List recomSocialList;
	/**
	 * 客户模板细项
	 */
	private List csSocialList;
	
	private SysUser getCurrentUser(){
		return (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
	}
	/**
	 * 已有员工社保档案概要翻页查询
	 * @return
	 */
	public String querycsEmplsocials(){
		SysUser u =  getCurrentUser();
		filter = getCsEmplsocialFilter();
		filter.setCompanyCode(u.getCompanyCode());
		csEmplsocialResult = service.queryCsEmplsocial(filter);
		return SUCCESS;
	}
	/**
	 * 未社保员工信息翻页查询 
	 * @return
	 */
	public String queryCsEmployeeResult(){
		SysUser u =  getCurrentUser();
		HttpServletRequest request = ServletActionContext.getRequest();
		if (pbEmployeeFilter==null) {
			pbEmployeeFilter = new PbEmployeeFilter();
		}
		pbEmployeeFilter.setStartIndex(Parse.toInteger(request.getParameter(Constants.START) , 0));
		pbEmployeeFilter.setEndIndex(Parse.toInteger(request.getParameter(Constants.LIMIT), Constants.DEFAULT_PAGE_SIZE));
		pbEmployeeFilter.setCompanyCode(u.getCompanyCode());
		pbEmployeeResult = service.queryCsEmployee(pbEmployeeFilter);
		return SUCCESS;
	}
	/**
	 * 批量调整社保
	 * @return
	 */
	public String adjustSocial(){
		SysUser user = this.getCurrentUser();
		List<CsEmplsocial> list = new ArrayList<CsEmplsocial>();
		for (String id : csEmplsocial.getIds().split(",")) {
			CsEmplsocial temp = new CsEmplsocial();
			temp.setEmployee_id(Long.parseLong(id));
			temp.setCompany_base_default(csEmplsocial.getCompany_base_default());
			temp.setCompany_percent_default(csEmplsocial.getCompany_percent_default());
			temp.setPerson_base_default(csEmplsocial.getPerson_base_default());
			temp.setPerson_percent_default(csEmplsocial.getPerson_percent_default());
			temp.setSocial_security_id(csEmplsocial.getSocial_security_id());
			temp.setModifier_code(user.getEmployeeCsCode());
			temp.setModifier_id(user.getEmployeeId());
			temp.setModifier_ip(user.getLoginIP());
			temp.setCreate_code(user.getEmployeeCsCode());
			temp.setCreator_id(user.getEmployeeId());
			temp.setCreator_ip(user.getLoginIP());
			list.add(temp);
		}
		try {
			service.batchAdjustSocial(list);
			successFlag = "1";
		} catch (Exception e) {
			e.printStackTrace();
			successFlag = "0";
		}
		return SUCCESS;
	}
	/**
	 * 社保项目翻页查询
	 * @return
	 */
	public String queryPbSocialItemsResult(){
		filter = getCsEmplsocialFilter();
		csSocialResult = service.queryPbSocialItemsResult(filter);
		return SUCCESS;
	}
	/**
	 * 调整社保中查询员工
	 * @return
	 */
	public String queryEmpBySocialItem(){
		SysUser user = this.getCurrentUser();
		try {
			if(filter == null)
				filter = new CsEmplsocialFilter();
			filter.setCompanyCode(user.getCompanyCode());
			recomSocialList = service.getEmployeeBySocialItem(filter);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 推荐社保模板翻页查询
	 * @return
	 */
	public String queryPbsocialTemplateResult(){
		filter = getCsEmplsocialFilter();
		csSocialResult = service.queryRecomSocialTemplate(filter);
		return SUCCESS;
	}
	
	/**
	 * 推荐社保模板细项翻页查询
	 * @return
	 */
	public String queryPbsocialTemplateDetailsResult(){
		filter = getCsEmplsocialFilter();
		recomSocialList = service.queryRecomSocialTemplateDetail(filter);
		return SUCCESS;
	}

	/**
	 * 客户社保模板翻页查询
	 * @return
	 */
	public String queryCsSocialTemplateResult(){
		filter = getCsEmplsocialFilter();
		csSocialResult = service.queryCsSocialTemplate(filter);
		return SUCCESS;
	}
	
	/**
	 * 客户社保模板细项翻页查询
	 * @return
	 */
	public String queryCsSocialTemplateDetailsResult(){
		filter = getCsEmplsocialFilter();
		csSocialList = service.queryCsSocialTemplateDetail(filter);
		return SUCCESS;
	}
	/**
	 * 员工社保档案详细信息翻页查询
	 * @return
	 */
	public String querycsEmplsocialDetails(){
		filter = getCsEmplsocialFilter();
		csEmplsocialResult = service.querycsEmplsocialDetails(filter);
		return SUCCESS;
	}
	/**
	 * 添加员工社保档案
	 * @return
	 */
	public String addEmpSocialI(){
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser u =  getCurrentUser();
		Long empId = null;
		if (StringUtils.isNotEmpty(request.getParameter("emp_code"))) {
			empId = Long.parseLong(request.getParameter("emp_code"));
		}
		if(empId == null){
			logger.fatal("雇员编号不能为空!");
			return ERROR;
		}
		String adds = request.getParameter("adds");
		List<CsEmplsocial> addsList=new ArrayList<CsEmplsocial>();
		JSONArray modifiedArray = JSONArray.fromObject(adds);
		for (int i = 0; i < modifiedArray.size(); i++) {
			   Object o = modifiedArray.get(i);
			   JSONObject jsonObject = JSONObject.fromObject(o);
			   CsEmplsocial csEmplsocial = (CsEmplsocial) JSONObject.toBean(jsonObject, CsEmplsocial.class);
			   csEmplsocial = initEmp(csEmplsocial, u);
			   csEmplsocial.setEmployee_id(empId);
			   addsList.add(csEmplsocial);
		}
		
		try {
			System.out.println(addsList.size());
			service.insertCsEmplsocial(addsList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 修改员工社保档案
	 * @return
	 */
	public String updateCsEmpsocial(){
		HttpServletRequest request = ServletActionContext.getRequest();
		SysUser u = getCurrentUser();
		Long empId = null;
		if (StringUtils.isNotEmpty(request.getParameter("emp_code"))) {
			empId = Long.parseLong(request.getParameter("emp_code"));
		}
		if(empId == null){
			logger.fatal("雇员编号不能为空!");
			return ERROR;
		}
		String adds = request.getParameter("adds");
		List<CsEmplsocial> addsList=new ArrayList<CsEmplsocial>();
		List<CsEmplsocial> modifList=new ArrayList<CsEmplsocial>();
		JSONArray modifiedArray = JSONArray.fromObject(adds);
		for (int i = 0; i < modifiedArray.size(); i++) {
			   Object o = modifiedArray.get(i);
			   JSONObject jsonObject = JSONObject.fromObject(o);
			   CsEmplsocial csEmplsocial = (CsEmplsocial) JSONObject.toBean(jsonObject, CsEmplsocial.class);
			   csEmplsocial = initEmp(csEmplsocial, u);
			   csEmplsocial.setEmployee_id(empId);
			   if(csEmplsocial.getEmployee_ss_id()== null){
				   addsList.add(csEmplsocial);
			   }else{
				   modifList.add(csEmplsocial);
			   }
		}
		String deleteds = request.getParameter("deleted");
		List<CsEmplsocial> delsList=new ArrayList<CsEmplsocial>();
		JSONArray delsArray = JSONArray.fromObject(deleteds);
		for (int i = 0; i < delsArray.size(); i++) {
			   Object o = delsArray.get(i);
			   JSONObject jsonObject = JSONObject.fromObject(o);
			   CsEmplsocial csEmplsocial = (CsEmplsocial) JSONObject.toBean(jsonObject, CsEmplsocial.class);
			   csEmplsocial = initEmp(csEmplsocial, u);
			   csEmplsocial.setEmployee_id(empId);
			   delsList.add(csEmplsocial);
		}
		
		try {
			service.updateCsEmplsocial(addsList, modifList, delsList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	private CsEmplsocial initEmp(CsEmplsocial csEmplsocial, SysUser u) {
		csEmplsocial.setCreate_code(u.getEmployeeCsCode());
		csEmplsocial.setCreator_id(u.getEmployeeId());
		csEmplsocial.setCreator_ip(u.getLoginIP());
		csEmplsocial.setModifier_code(u.getEmployeeCsCode());
		csEmplsocial.setModifier_id(u.getEmployeeId());
		csEmplsocial.setModifier_ip(u.getLoginIP());
		csEmplsocial.setCompany_code(u.getCompanyCode());
		
		return csEmplsocial;
	}
	/**
	 * 检查员工的社保项目是否已经计算过
	 * @return
	 */
	public String checkEmpSSocialItemIsCaled(){
		try {
			msg = service.checkEmpSSocialItemIsCaled(csEmplsocial);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 不计入结果操作，不要笑这个方法名，实在不知道怎么取名-_-||
	 * @return
	 */
	public String bjrjg(){
		try {
			service.bjrjg(csEmplsocial);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**********************************************************************
	 * 
	 * getter, setter method
	 * 
	 **********************************************************************/

	public CsEmplsocial getCsEmplsocial() {
		return csEmplsocial;
	}

	public void setCsEmplsocial(CsEmplsocial csEmplsocial) {
		this.csEmplsocial = csEmplsocial;
	}

	public Page getCsEmplsocialResult() {
		return csEmplsocialResult;
	}

	public void setCsEmplsocialResult(Page csEmplsocialResult) {
		this.csEmplsocialResult = csEmplsocialResult;
	}

	public Page getCsSocialResult() {
		return csSocialResult;
	}

	public void setCsSocialResult(Page csSocialResult) {
		this.csSocialResult = csSocialResult;
	}

	public String getEmpSsIds() {
		return empSsIds;
	}

	public void setEmpSsIds(String empSsIds) {
		this.empSsIds = empSsIds;
	}


	public PbSocialMasterAndDetail getPbSocialMasterAndDetail() {
		return pbSocialMasterAndDetail;
	}

	public void setPbSocialMasterAndDetail(
			PbSocialMasterAndDetail pbSocialMasterAndDetail) {
		this.pbSocialMasterAndDetail = pbSocialMasterAndDetail;
	}

	public void setService(CsEmplsocialService service) {
		this.service = service;
	}

	public CsEmplsocialFilter getFilter() {
		return filter;
	}

	public void setFilter(CsEmplsocialFilter filter) {
		this.filter = filter;
	}

	public Page getPbEmployeeResult() {
		return pbEmployeeResult;
	}

	public void setPbEmployeeResult(Page pbEmployeeResult) {
		this.pbEmployeeResult = pbEmployeeResult;
	}

	public CsSocialMaster getCsSocialMaster() {
		return csSocialMaster;
	}

	public void setCsSocialMaster(CsSocialMaster csSocialMaster) {
		this.csSocialMaster = csSocialMaster;
	}

	public CsSocialDetail getCsSocialDetail() {
		return csSocialDetail;
	}

	public void setCsSocialDetail(CsSocialDetail csSocialDetail) {
		this.csSocialDetail = csSocialDetail;
	}

	public PbSocialDetail getPbSocialDetail() {
		return pbSocialDetail;
	}

	public void setPbSocialDetail(PbSocialDetail pbSocialDetail) {
		this.pbSocialDetail = pbSocialDetail;
	}

	public CsEmplsocial getCsEmplsocial1() {
		return csEmplsocial1;
	}

	public void setCsEmplsocial1(CsEmplsocial csEmplsocial1) {
		this.csEmplsocial1 = csEmplsocial1;
	}

	public PbCommsocialMaster getPbCommsocialMaster() {
		return pbCommsocialMaster;
	}

	public void setPbCommsocialMaster(PbCommsocialMaster pbCommsocialMaster) {
		this.pbCommsocialMaster = pbCommsocialMaster;
	}

	public PbEmployee getPbEmployee1() {
		return pbEmployee1;
	}

	public void setPbEmployee1(PbEmployee pbEmployee1) {
		this.pbEmployee1 = pbEmployee1;
	}

	/**
	 * @return 返回pbEmployeeFilter
	 */
	public PbEmployeeFilter getPbEmployeeFilter() {
		return pbEmployeeFilter;
	}
	/**
	 * @param pbEmployeeFilter 设置pbEmployeeFilter
	 */
	public void setPbEmployeeFilter(PbEmployeeFilter pbEmployeeFilter) {
		this.pbEmployeeFilter = pbEmployeeFilter;
	}
	
	private CsEmplsocialFilter getCsEmplsocialFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		if (filter==null) {
			filter = new CsEmplsocialFilter();
		}
		filter.setStartIndex(Parse.toInteger(request.getParameter(Constants.START) , 0));
		filter.setEndIndex(Parse.toInteger(request.getParameter(Constants.LIMIT), Constants.DEFAULT_PAGE_SIZE));
		return filter;
	}
	/**
	 * @return 返回客户模板细项
	 */
	public List getCsSocialList() {
		return csSocialList;
	}
	/**
	 * @param csSocialList 设置客户模板细项
	 */
	public void setCsSocialList(List csSocialList) {
		this.csSocialList = csSocialList;
	}
	/**
	 * @return 返回推荐模板细项
	 */
	public List getRecomSocialList() {
		return recomSocialList;
	}
	/**
	 * @param recomSocialList 设置推荐模板细项
	 */
	public void setRecomSocialList(List recomSocialList) {
		this.recomSocialList = recomSocialList;
	}
	/**
	 * @return 返回successFlag
	 */
	public String getSuccessFlag() {
		return successFlag;
	}
	/**
	 * @param successFlag 设置successFlag
	 */
	public void setSuccessFlag(String successFlag) {
		this.successFlag = successFlag;
	}
	public String getMsg() {
		return msg=msg==null?"":msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
}
